package com.fingard.dsp.bank.directbank.cbhb02;

import com.fingard.constant.Format;
import com.fingard.dsp.bank.directConfig.ActSetItem;
import com.fingard.dsp.bank.directbank.FailCode;
import com.fingard.dsp.bank.directbank.PaymentState;
import com.fingard.dsp.bank.directbank.bankInterface.ISinglePay;
import com.fingard.dsp.bank.reqrespobj.single.*;
import com.fingard.dsp.bank.reqrespobj.single.detail.RetAtsSglDetail;
import com.fingard.io.FileHelper;
import com.fingard.net.ConnState;
import com.fingard.text.StringHelper;
import com.fingard.xml.XElement;
import com.fingard.xml.XmlTextReader;
import com.fingard.xml.XmlTextWriter;

import java.net.URL;
import java.util.HashMap;

public class CBHB02SinglePay extends CBHB02Base implements ISinglePay {
    @Override
    public RetAtsSingle d_1908_SglPay(ReqSingle p_req1908) throws Exception {
        //返回ATS的对象
        RetAtsSingle retAts = new RetAtsSingle(p_req1908.reqHeader);
        RetAtsSglDetail tmpRetDetail;
        tmpRetDetail = d_190802(p_req1908);

        if(tmpRetDetail.respCode.equals("-1") && StringHelper.isNullOrEmpty(tmpRetDetail.reqSeqID)){
            tmpRetDetail.reqSeqID = p_req1908.singleDetail.askNumber;
        }
        retAts.writeRetDetail(tmpRetDetail);
        return retAts;
    }
    private RetAtsSglDetail d_190802(ReqSingle p_req1908) throws Exception {
        RetAtsSglDetail retDetail = new RetAtsSglDetail();
        try {
            ActSetItem actItem = getBankConfig().getActSetItem(p_req1908.singleDetail.payAct, "");
            if (actItem == null) {
                retDetail.respCode = "0";
                retDetail.respInfo = "未找到账号[" + p_req1908.singleDetail.recAct + "]商户配置信息!";
                retDetail.transState = PaymentState.FAIL_Payment;
                retDetail.payInfoCode = FailCode.E9981;
                return retDetail;
            }
            //赋值通用参数
            CBHB02Header bankHeader = new CBHB02Header();
            bankHeader.transType = "SCIP";
            bankHeader.instSeq = p_req1908.singleDetail.askNumber;
            XmlTextWriter xmlWriter = new XmlTextWriter();//发银行报文的XML对象
            xmlWriter.autoFormat = false;
            //写入通用的头部节点
            writeCommonHeader(xmlWriter, bankHeader);
            xmlWriter.writeElementString("pyrAct", p_req1908.singleDetail.payAct);
            xmlWriter.writeElementString("pyrNam", p_req1908.singleDetail.payName);
            xmlWriter.writeElementString("pyeAct", p_req1908.singleDetail.recAct);
            xmlWriter.writeElementString("pyeNam", p_req1908.singleDetail.recName);
            xmlWriter.writeElementString("pyeBnk", p_req1908.singleDetail.cnapsCode);
            xmlWriter.writeElementString("amt", p_req1908.singleDetail.payAmount);
            xmlWriter.writeElementString("postscript", p_req1908.singleDetail.memo);
            xmlWriter.writeElementString("cardFlg", "1");
            xmlWriter.writeElementString("realFlg", "1");
            if(!"1".equals(p_req1908.singleDetail.isPrivate)){
                xmlWriter.writeElementString("accType", "04");
                xmlWriter.writeElementString("comDebitCardAccName", p_req1908.singleDetail.payName);
                xmlWriter.writeElementString("businessLicenseRegNo", "04");
            }else {
                xmlWriter.writeElementString("accType", "01");
            }
            xmlWriter.writeStartDocByDeclaration("</" + bankHeader.transType + "Req>");
            xmlWriter.writeEndElement();
            String[] tmpStrRet = sendToBank(xmlWriter.toXmlString());
            if (tmpStrRet[0].length() == 0) {
                StringBuilder tmpSbMsg = new StringBuilder();
                XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], tmpSbMsg);
                if (tmpBkReader != null) {
                    String tmpRetCode = tmpBkReader.getFirstText("/Cbhbpac/Message/"+bankHeader.transType+"Res/rspCode");
                    String tmpRetInfo = tmpBkReader.getFirstText("/Cbhbpac/Message/"+bankHeader.transType+"Res/rspMsg");
                    retDetail.respCode = "0";
                    retDetail.respInfo = tmpRetInfo;
                    retDetail.reqSeqID = p_req1908.singleDetail.askNumber;
                    retDetail.transState = tmpRetCode;
                    retDetail.payInfoCode = tmpRetCode;
                    retDetail.transAmt = p_req1908.singleDetail.payAmount;
                } else {
                    retDetail.respCode = "-1";
                    retDetail.respInfo = tmpSbMsg.toString();
                }
            } else if (tmpStrRet[0].equals(ConnState.FAIL)) {
                retDetail.respCode = "0";
                retDetail.respInfo = tmpStrRet[1];
                retDetail.reqSeqID = p_req1908.singleDetail.askNumber;
                retDetail.transState = PaymentState.FAIL_Payment;
                retDetail.payInfoCode = tmpStrRet[0];
            } else {
                retDetail.respCode = "-1";
                retDetail.respInfo = tmpStrRet[1];
            }
        } catch (Exception e) {
            retDetail.respCode = "-1";
            retDetail.respInfo = e.toString();
            e.printStackTrace();
        }
        return retDetail;
    }

    @Override
    public RetAtsSingle d_1944_QrySglPay(ReqQrySingle p_req1944) throws Exception {
        RetAtsSingle retAts = new RetAtsSingle(p_req1944.reqHeader);
        RetAtsSglDetail retDetail = new RetAtsSglDetail();
        CBHB02Header bankHeader = new CBHB02Header();
        bankHeader.transType = "SCTQ";
        bankHeader.instSeq = p_req1944.askNumber;
        XmlTextWriter xmlWriter = new XmlTextWriter();//发银行报文的XML对象
        xmlWriter.autoFormat = false;
        //写入通用的头部节点
        writeCommonHeader(xmlWriter, bankHeader);
        xmlWriter.writeEndDocument();

        String[] tmpStrRet = sendToBank(xmlWriter.toXmlString());
        if (tmpStrRet[0].length() == 0) {
            StringBuilder tmpSbMsg = new StringBuilder();
            XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], tmpSbMsg);
            if (tmpBkReader != null) {
                String tmpRetCode = tmpBkReader.getFirstText("/Cbhbpac/Message/"+bankHeader.transType+"Res/rspCode");
                String tmpRetInfo = tmpBkReader.getFirstText("/Cbhbpac/Message/"+bankHeader.transType+"Res/rspMsg");
                retDetail.respCode = "0";
                retDetail.respInfo = tmpRetInfo;
                retDetail.reqSeqID = p_req1944.askNumber;
                retDetail.transState = tmpRetCode;
                retDetail.payInfoCode = tmpRetCode;
                retDetail.transAmt = p_req1944.payAmount;
            } else {
                retDetail.respCode = "-1";
                retDetail.respInfo = tmpSbMsg.toString();
            }
        } else if (tmpStrRet[0].equals(ConnState.FAIL)) {
            retDetail.respCode = "0";
            retDetail.respInfo = tmpStrRet[1];
            retDetail.reqSeqID = p_req1944.askNumber;
            retDetail.transState = PaymentState.FAIL_Payment;
            retDetail.payInfoCode = tmpStrRet[0];
        } else {
            retDetail.respCode = "-1";
            retDetail.respInfo = tmpStrRet[1];
        }

        retAts.writeRetDetail(retDetail);
        return retAts;
    }

    @Override
    public RetAtsSingleBat d_1908BAT_SglBatPay(ReqSingleBat p_req1908BAT) throws Exception {
        return null;
    }

    @Override
    public RetAtsSingleBat d_1944BAT_QrySglBatPay(ReqQrySingleBat p_req1944BAT) throws Exception {
        return null;
    }
}
